package org.zht.dormpro.dorm.activemq;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

@Log4j
public class Sender {
    @Autowired
    private JmsTemplate jmsTemplate;

    public boolean sendMsg(final JSONObject msg){
        boolean success = false;
        try {
            MessageCreator messageCreator = new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage(msg.toJSONString());
                }
            };
            jmsTemplate.send(jmsTemplate.getDefaultDestination(),messageCreator);
            success = true;
        }  catch (JmsException e) {
            log.error("消息发送失败!",e);
        }
        return success;
    }

}
